{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "db4fdff0",
   "metadata": {},
   "source": [
    "---  \n",
    "2022/10/11/ week08  \n",
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d332deed",
   "metadata": {},
   "source": [
    "# 本周内容  \n",
    "> 0.代码的注释  \n",
    "> 1.用户输入  \n",
    "> 2.格式化文本信息"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28128bf9",
   "metadata": {},
   "source": [
    "### 0.代码的注释  \n",
    "> 1.#,一般写在代码块的最上边，或者每一行代码的最后，用于代码思路的记录。  \n",
    "> 2.'''注释内容'''.一般是注释代码块。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7b9ecff3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "三个引号注释代码块内容，不运行\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "注释\n",
    "'''\n",
    "print(\"三个引号注释代码块内容，不运行\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9278252c",
   "metadata": {},
   "source": [
    "### 1.用户输入  \n",
    "> 1.input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6662f1e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:1\n",
      "密码:123\n",
      "1\n",
      "123\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统\n",
    "username = input(\"账号:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "print(username)\n",
    "print(password)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1ea409e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:admin\n",
      "密码:123456\n",
      "欢迎admin登录！\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统 迭代1 ：增加了判断功能\n",
    "# 假设我们数据库有用户信息：账号：admin，密码，123456\n",
    "user1 = \"admin\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "username = input(\"账号:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "# 情景一：输入信息正确，登陆成功\n",
    "if user1 == username and password1 == password:\n",
    "    print(\"欢迎\"+user1+\"登录！\")\n",
    "    \n",
    "# 情景二：账号不存在或密码不正确\n",
    "else:\n",
    "    print(\"账号不存在或密码不正确!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "152bcd94",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:1\n",
      "密码:12\n",
      "账号不存在或密码不正确!\n",
      "您还有2次尝试的机会\n",
      "账号:1\n",
      "密码:12\n",
      "账号不存在或密码不正确!\n",
      "您还有1次尝试的机会\n",
      "账号:1\n",
      "密码:12\n",
      "三次输入失败，请1小时后再来尝试\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统 迭代2 ：增加循环功能\n",
    "# 假设我们数据库有用户信息：账号：admin，密码，123456\n",
    "user1 = \"admin\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "for i in range(3,0,-1):  # 循环3次，i 是一个 int\n",
    "    username = input(\"账号:\")\n",
    "    password = input(\"密码:\")\n",
    "\n",
    "    # 情景一：输入信息正确，登陆成功\n",
    "    if user1 == username and password1 == password:\n",
    "        print(\"欢迎\"+user1+\"登录！\")\n",
    "        break # 终止循环\n",
    "        \n",
    "    # 情景二：账号不存在或密码不正确\n",
    "    else:\n",
    "        if i == 1: # i=1,即没有重试机会\n",
    "            print(\"三次输入失败，请1小时后再来尝试\")\n",
    "        else:\n",
    "            print(\"账号不存在或密码不正确!\")\n",
    "            print(\"您还有\"+str(i-1)+\"次尝试的机会\") # str强制将int转换成str"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f28577d",
   "metadata": {},
   "source": [
    "###  2.格式化输出  \n",
    "> 1.字符串拼接  \n",
    "> 2.占位符  \n",
    "> 3.format()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2a92f5c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：wei\n",
      "年龄：18\n",
      "工作：无业\n",
      "收入：-1000\n",
      "地区：广东\n",
      "-----INFO OF wei------\n",
      "年龄：18\n",
      "工作：无业\n",
      "收入：-1000\n",
      "地区：广东\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法一：字符串拼接\n",
    "name = input(\"姓名：\")\n",
    "age = input(\"年龄：\")\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = '''-----INFO OF '''+ name +'''------'''+'''\n",
    "年龄：'''+age+'''\n",
    "工作：'''+job+'''\n",
    "收入：'''+salary+'''\n",
    "地区：'''+reg\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0e2886c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年龄：18\n",
      "18 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# input 作为用户输入，其都以字符串形式返回\n",
    "age = input(\"年龄：\")\n",
    "print(age,type(age))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3267d0e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：1\n",
      "年龄：1\n",
      "工作：1\n",
      "收入：1\n",
      "地区：1\n",
      "-----INFO OF 1------\n",
      "姓名：1\n",
      "年龄：1\n",
      "工作：1\n",
      "收入：1\n",
      "地区：1\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法二：占位符 %s str,   %d int ,  依次有序输入\n",
    "name = input(\"姓名：\")\n",
    "age = int(input(\"年龄：\")) # 强制将 str 转换成 int\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = '''-----INFO OF %s------\n",
    "姓名：%s\n",
    "年龄：%d\n",
    "工作：%s\n",
    "收入：%s\n",
    "地区：%s'''%(name,name,age,job,salary,reg)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "45a5a368",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：1\n",
      "年龄：2\n",
      "工作：3\n",
      "收入：\n",
      "地区：\n",
      "-----INFO OF 1------\n",
      "姓名：1\n",
      "年龄：2\n",
      "工作：3\n",
      "收入：\n",
      "地区：\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法三：format() 格式化输出函数，参数传递，不需要考虑位置\n",
    "name = input(\"姓名：\")\n",
    "age = int(input(\"年龄：\")) # 强制将 str 转换成 int\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = '''-----INFO OF {姓名}------\n",
    "姓名：{姓名}\n",
    "年龄：{年龄}\n",
    "工作：{工作}\n",
    "收入：{收入}\n",
    "地区：{地区}'''.format(年龄 = age,姓名 = name,工作 = job,收入 = salary, 地区 = reg)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ca0129b",
   "metadata": {},
   "source": [
    "## 课后练习  \n",
    "* 要求：\n",
    "> 1.运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。  \n",
    "> 2.让用户输入商品编号进行商品的购买。  \n",
    "> 3.用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。  \n",
    "> 4.用户可以随时退出购买，退出时打印用户已购买的商品和支付宝余额。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "63596e94",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的余额20\n",
      "开始购物\n",
      "1 ['韭菜饺', 10]\n",
      "2 ['芹菜饺', 11]\n",
      "3 ['白菜饺', 8]\n",
      "请输入您所需要的商品编号（单选），或者按 r 退出购物1\n",
      "您要购买的是 韭菜饺\n",
      "您已购买韭菜饺,您的余额为10\n",
      "1 ['韭菜饺', 10]\n",
      "2 ['芹菜饺', 11]\n",
      "3 ['白菜饺', 8]\n",
      "请输入您所需要的商品编号（单选），或者按 r 退出购物2\n",
      "您要购买的是 芹菜饺\n",
      "您的余额不足，购买失败\n",
      "您现在的余额为:10\n",
      "1 ['韭菜饺', 10]\n",
      "2 ['芹菜饺', 11]\n",
      "3 ['白菜饺', 8]\n",
      "请输入您所需要的商品编号（单选），或者按 r 退出购物r\n",
      "已退出购物，您的余额为10\n",
      "以下是您所购买的商品['韭菜饺']\n"
     ]
    }
   ],
   "source": [
    "## 输入余额，余额格式错误处理\n",
    "while True:\n",
    "    balance_old = input(\"请输入您的余额\")\n",
    "    if not balance_old.isdigit():   # .isdigit，判断余额是否为数字\n",
    "        print(\"输入格式错误，请重新输入\")\n",
    "    else:\n",
    "        print(\"开始购物\")\n",
    "        balance_old = int(balance_old) # 将输入的字符转为数字\n",
    "        break\n",
    "balance_new = balance_old  # 设定一个新的余额变量，便于在未购买任何商品时打印购买列表\n",
    "balance_new = int(balance_new)\n",
    "\n",
    "    \n",
    "## 顾客选择商品\n",
    "\n",
    "# 商品列表\n",
    "product_list = [\n",
    "[\"韭菜饺\",10],    \n",
    "[\"芹菜饺\",11],\n",
    "[\"白菜饺\",8]    \n",
    "]\n",
    "\n",
    "shopping_list = []\n",
    "\n",
    "\n",
    "while True:\n",
    "    # 生成带序号的商品列表  enumerate,枚举\n",
    "    for index, item in enumerate(product_list):\n",
    "        print(index + 1, item)\n",
    "\n",
    "    enter = input(\"请输入您所需要的商品编号（单选），或者按 r 退出购物\")\n",
    "    if enter.isdigit():\n",
    "        enter = int(enter)\n",
    "\n",
    "        if enter <= len(product_list) and enter > 0:  \n",
    "            print(\"您要购买的是\",product_list[enter - 1][0])  ## 取列表的第一个值，既仅取商品名称\n",
    "            if balance_new >= product_list[enter - 1][1]:\n",
    "                shopping_list.append(product_list[enter - 1][0])\n",
    "                balance_new -= product_list[enter - 1][1]\n",
    "                print(\"您已购买\" + product_list[enter - 1][0] + \",您的余额为\" + str(balance_new))\n",
    "            else:\n",
    "                print (\"您的余额不足，购买失败\")\n",
    "                print(\"您现在的余额为:\" + str(balance_new))\n",
    "\n",
    "\n",
    "        else:\n",
    "            print(\"您所输入的编号为错误编号，请重新输入\")\n",
    "\n",
    "    elif enter == \"r\":\n",
    "        if balance_new == balance_old:\n",
    "            print(\"您没有购买任何商品，您的余额为\" + str(balance_new))\n",
    "        else:\n",
    "            print(\"已退出购物，您的余额为\" + str(balance_new))\n",
    "            print(\"以下是您所购买的商品\" + str(shopping_list))\n",
    "        break    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94189e04",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a830f546",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
